
// <copyright file="CharacterEncoder.cs" company="Subhadra Software Solutions">
//   
// </copyright>
// <summary>
//   Class CharacterEncoder.
// </summary>

namespace SubhadraSolutions.Sharp.Utils.Windows.Forms
{
    using System.Text;

    /// <summary>
    ///     Class CharacterEncoder.
    /// </summary>
    public class CharacterEncoder
    {
        #region Public Methods and Operators

        /// <summary>
        /// Encodes the specified original text.
        /// </summary>
        /// <param name="originalText">
        /// The original text.
        /// </param>
        /// <returns>
        /// System.String.
        /// </returns>
        public static string Encode(string originalText)
        {
            if (string.IsNullOrWhiteSpace(originalText))
            {
                return string.Empty;
            }

            var encodedText = new StringBuilder();
            foreach (char c in originalText)
            {
                switch (c)
                {
                    case '"':
                        encodedText.Append("&quot;");
                        break;

                    case '&':
                        encodedText.Append(@"&amp;");
                        break;

                    case '\'':
                        encodedText.Append(@"&apos;");
                        break;

                    case '<':
                        encodedText.Append(@"&lt;");
                        break;

                    case '>':
                        encodedText.Append(@"&gt;");
                        break;

                    // The character '\' should be converted to @"\\" or "\\\\"
                    case '\\':
                        encodedText.Append(@"\\");
                        break;

                    // The character '{' should be converted to @"\{" or "\\{"
                    case '{':
                        encodedText.Append(@"\{");
                        break;

                    // The character '}' should be converted to @"\}" or "\\}"
                    case '}':
                        encodedText.Append(@"\}");
                        break;

                    default:
                        encodedText.Append(c);
                        break;
                }
            }

            return encodedText.ToString();
        }

        #endregion Public Methods and Operators
    }
}
